package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TSingleselchoice;
import com.ruoyi.project.system.service.ITSingleselchoiceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TSinglesel;
import com.ruoyi.project.system.service.ITSingleselService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 单选题库Controller
 * 
 * @author ruoyi
 * @date 2020-05-09
 */
@RestController
@RequestMapping("/system/singlesel")
public class TSingleselController extends BaseController
{
    @Autowired
    private ITSingleselService tSingleselService;
    @Autowired
    private ITSingleselchoiceService itSingleselchoiceService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询单选题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSinglesel tSinglesel)
    {
        startPage();
        List<TSinglesel> list = tSingleselService.selectTSingleselList(tSinglesel);
        return getDataTable(list);
    }

    /**
     * 导出单选题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:export')")
    @Log(title = "单选题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSinglesel tSinglesel)
    {
        List<TSinglesel> list = tSingleselService.selectTSingleselList(tSinglesel);
        ExcelUtil<TSinglesel> util = new ExcelUtil<TSinglesel>(TSinglesel.class);
        return util.exportExcel(list, "singlesel");
    }

    /**
     * 获取单选题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:query')")
    @GetMapping(value = "/{singleselId}")
    public AjaxResult getInfo(@PathVariable("singleselId") Integer singleselId)
    {
        return AjaxResult.success(tSingleselService.selectTSingleselById(singleselId));
    }

    /**
     * 新增单选题库
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:add')")
    @Log(title = "单选题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSinglesel tSinglesel)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        tSinglesel.setUserId(user.getUserId().intValue());
        tSingleselService.insertTSinglesel(tSinglesel);
        System.out.println("tSinglesel = " + tSinglesel);
        return toAjax( tSingleselService.addMiddle(tSinglesel.getCourseId(),tSinglesel.getSingleselId()));
    }

    /**
     * 修改单选题库
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:edit')")
    @Log(title = "单选题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSinglesel tSinglesel)
    {
        return toAjax(tSingleselService.updateTSinglesel(tSinglesel));
    }

    /**
     * 删除单选题库
     */
    @PreAuthorize("@ss.hasPermi('system:singlesel:remove')")
    @Log(title = "单选题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{singleselIds}")
    public AjaxResult remove(@PathVariable Integer[] singleselIds)
    {
        return toAjax(tSingleselService.deleteTSingleselByIds(singleselIds));
    }
    /**
     * 查询多选题相应的选项
     */
    @PostMapping("/choice")
    @ResponseBody
    public AjaxResult getMultiselChoiceList(Integer multiselId )
    {
        TSingleselchoice tSingleselchoice = new TSingleselchoice();
        tSingleselchoice.setQuestionId(multiselId);
        List<TSingleselchoice> tSingleselchoices = itSingleselchoiceService.selectTSingleselchoiceList(tSingleselchoice);
        if(tSingleselchoices!=null&&tSingleselchoices.size()>0){
            return AjaxResult.success(tSingleselchoices);
        }else {
            return AjaxResult.error();
        }
    }
}
